/**
 *author:yhb
 */
package com.towker.frame.service.sys.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;

import com.towker.core.db.*;
import com.towker.core.encrypt.EncryptUtil;
import com.towker.core.page.*;
import com.towker.frame.dao.rbac.*;
import com.towker.frame.dao.sys.UserDao;
import com.towker.frame.model.rbac.RoleUser;
import com.towker.frame.model.sys.User;
import com.towker.frame.share.ServiceShare;

@Service
public class UserService extends BaseService<User, java.lang.Long>{
	private UserDao userDao;
	private RoleUserDao roleUserDao;
	public void setUserDao(UserDao userDao){this.userDao = userDao;}
	public void setRoleUserDao(RoleUserDao roleUserDao){this.roleUserDao = roleUserDao;}
	@SuppressWarnings("unchecked")
	@Override
	protected EntityDao getEntityDao(){return (EntityDao) this.userDao;}
	/**管理员重置用户密码*/
	public void resetPwd(long userId, String password) throws Exception{
		password = EncryptUtil.encryptMd5(password);
		this.userDao.resetPwd(userId, password);
	}
	/**获得当前用户的所有上级*/
	@SuppressWarnings("unchecked")
	public List<User> getAllLeadsByCrtUserid(long userid){
		List<User> list=userDao.getLeadsByUserid(userid);
		return list;
	}
	/**获得某个下属的所有上级*/
	@SuppressWarnings("unchecked")
	public List<User> getAllLeadsBySuborid(long suborid){
		List<User> list=userDao.getAllLeadsBySuborid(suborid);
		return list;
	}
	/**获得当前用户的所有下属*/
	@SuppressWarnings("unchecked")
	public List<User> getAllSuborsByCrtUserid(long crtUserid){
		List<User> list=userDao.getAllSuborsByCrtUserid(crtUserid);
		return list;
	}
	 /**获得某个上级的所有下属*/
	@SuppressWarnings("unchecked")
	public List<User> getAllSuborsByLeadid(long leadid){
		List<User> list=userDao.getAllSuborsByLeadid(leadid);
		return list;
	}
	/**获得所有用户json数组*/
	public String getAllUser(){
		List<User> list=userDao.getAllUser();
		if(null==list||0==list.size())return"[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**根据角色ID获得已分配部门的用户*/
	public String getAssigendUserByRoleid(long roleid){
		List<User> list=userDao.getAssigendUserByRoleid(roleid);
		if(null==list||0==list.size())return"[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	public String getAssignedUserByUserid(long userid){
		List<User> list=userDao.getAssignedUserByUserid(userid);
		if(null==list||0==list.size())return"[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**根据角色编号，获得分配的用户*/
	public List<User> getAssignedUsers(long roleid){
		List<User> list=userDao.getAssignedUsers(roleid);
		List<RoleUser> list2=roleUserDao.getAssignedUserByRoleid(roleid);
		for(User tmp:list){
			for(RoleUser tmp2:list2){
				if(tmp2.getUserid().equals(tmp.getId())){
					tmp.setId(tmp2.getId());
				}
			}
		}
		return list;
	}
	
	public User getById(long id){
		return userDao.getById(id);
		
	}
	
	
	/**获得员工列表，通过部门编号*/
	public List<User> getDeptUser(long deptid){
		return this.userDao.getUsersByDeptid(deptid);
	}
	/**获得部门用户分页*/
	@SuppressWarnings("unchecked")
	public PageModel getDeptUserPage(int currentPage, Map map){
		int pageSize=5;
		PageRequest request=PageRequest.getPageRequest(currentPage, pageSize, map);
		PageModel page = userDao.getDeptUserPage(request);
		return page;
	}
	/**获得角色用户分页*/
	@SuppressWarnings("unchecked")
	public PageModel getRoleUserPage(int currentPage, Map map){
		int pageSize=5;
		PageRequest request=PageRequest.getPageRequest(currentPage, pageSize, map);
		PageModel page = userDao.getRoleUserPage(request);
		return page;
	}
	/**获得下属的分页模型*/
	@SuppressWarnings("unchecked")
	public PageModel<User>  getSubordinate(int currentPageSize,int pageSize,Map map){
		PageRequest request=new PageRequest();
		request.setCurrentPage(currentPageSize);
		request.setPageSize(pageSize);
		request.setFilters(map);
		return null;
		//return (PageModel<User>) userDao.getPage("getSubordinate", "getSubordinateCount", request);
	}
	/**获得下属分页*/
	@SuppressWarnings("unchecked")
	public PageModel getSuborPage(int currentPage, Map map){
		int pageSize=5;
		PageRequest request=PageRequest.getPageRequest(currentPage, pageSize, map);
		//PageModel page = userDao.getSuborPage(request);
		return null;
	}
	/**根据部门ID获得未分配用户*/
	public String getUnassignedUserByDeptid(long deptid){
		List<User> list=userDao.getUnassignedUserByDeptid(deptid);
		if(null==list||0==list.size())return "[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**根据角色ID获得未分配部门的用户*/
	public String getUnassignedUserByRoleid(long roleid){
		List<User> list=userDao.getUnassignedUserByRoleid(roleid);
		if(null==list||0==list.size())return"[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**获得未下属分配用户*/
	public String getUnassignedUserByUserid(long userid){
		List<User> list=userDao.getUnassignedUserByUserid(userid);
		if(null==list||0==list.size())return"[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**根据部门编号获得所有用户*/
	public String getAssignedUserByDeptid(long deptid){
		List<User> list=userDao.getAssignedUserByDeptid(deptid);
		if(null==list||0==list.size())return"[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**获得用户json数据*/
	public String getUserJson(){
		List<User> list=userDao.getAllUser();
		if(null==list||0==list.size())return "[]";
		String str="[";
		for(User tmp:list)str+=tmp+",";
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**获得用户分页*/
	@SuppressWarnings("unchecked")
	public PageModel getUserPage(int currentPage, Map map){
		int pageSize=5;
		PageRequest request=PageRequest.getPageRequest(currentPage, pageSize, map);
		PageModel page = userDao.getUserPage(request);
		return page;
	}
	/** 根据部门编号，获得该部门的所有用户*/
	@SuppressWarnings("unchecked")
	public List<User> getUsersByDeptid(long deptid){
		return userDao.getUsersByDeptid(deptid);
	}
	/**根据角色编号获得该角色的所有用户*/
	@SuppressWarnings("unchecked")
	public List<User> getUsersByRoleid(long roleid){
		return userDao.getUsersByRoleid(roleid);
	}
	@SuppressWarnings("unchecked")
	public static void main(String[] args){
		UserService service=(UserService)ServiceShare.getService(UserService.class);
		Map map=new HashMap();
		map.put("leadid", 1);
		PageModel<User> pageModel = service.getSubordinate(1, 10, map);
	}
	
	/**
	 * 
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public int getCount(long pid)
	{
		Map map=new HashMap();
		map.put("pid", pid);
		PageRequest pageRequest=new PageRequest();
		pageRequest.setFilters(map);
		return userDao.getCount(pageRequest);
	}
	
	@SuppressWarnings("unchecked")
	public int updateUser(String account,String pwd,String name,long id)
	{
		Map map = new HashMap();
		map.put("account", account);
		map.put("password", pwd);
		map.put("name", name);
		map.put("id", id);
		return userDao.updateUser(map);
	}
	
	public List<User> getCustomCodes() {
		return userDao.getCustomCodes();
	}
	
	public String getCustomCode(String custom_code) {
		User user = userDao.getByCustomCode(custom_code);
		return user.getCustomCode();
	}
	
	@SuppressWarnings("unchecked")
	public void updSql(Map map){
		 userDao.updSql(map);
	}
}
